﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.DataModel.WorkFlow;
using System.Data.SqlClient;
using System.Data;
using CommonLibrary.DB;

namespace ServiceApplication.Dao.WorkFlow
{
    class WftypeDao
    {
        public int AddEntity(WfTypeEntity wfTypeEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftype_id", wfTypeEntity.wftype_id));
            parameterList.Add(new SqlParameter("@wftype_name", wfTypeEntity.wftype_name));
            parameterList.Add(new SqlParameter("@wftype_status", wfTypeEntity.wftype_status));

            string sql = "insert into wf_wftype (wftype_name,wftype_status) values (@wftype_name,@wftype_status); " +
                " select @@identity";
            string id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(id);
        }

        public WfTypeEntity QueryEntity(int wftype_id)
        {
            WfTypeEntity wfTypeEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftype_id", wftype_id));
            string sql = "select wftype_id,wftype_name,wftype_status  from wf_wftype where wftype_id=@wftype_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (null == result || result.Length == 0) return null;

            wfTypeEntity = new WfTypeEntity();
            wfTypeEntity.wftype_id = int.Parse(result[0][0]);
            wfTypeEntity.wftype_name = result[0][1];
            wfTypeEntity.wftype_status = result[0][2];
            return wfTypeEntity;
        }

        public void UpadateEntity(WfTypeEntity wfTypeEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftype_id", wfTypeEntity.wftype_id));
            parameterList.Add(new SqlParameter("@wftype_name", wfTypeEntity.wftype_name));
            parameterList.Add(new SqlParameter("@wftype_status", wfTypeEntity.wftype_status));
            string sql = "update wf_wftype set wftype_name=@wftype_name,wftype_status=@wftype_status where wftype_id=@wftype_id ";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="task_id"></param>
        /// <returns></returns>
        public DataTable QueryAll()
        {
            string sql = "select wftype_id 编码,wftype_name 名称,wftype_status 状态 from wf_wftype";

            DataTable dt = DBExecuteUtil.querySqlTable(sql);
            return dt;
        }


    }
}
